﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class FinancePropController : AdminBaseController
    {
        //
        // GET: /FinanceProp/
        /// <summary>
        /// 代码维护主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_FinanceProp", Order = 1)]
        [LogPageActionFilter("资金性质管理", Order = 2)]
        public ActionResult Index()
        {
            List<Model.FinanceProp> codeNameList = new List<Model.FinanceProp>();
            ViewData["blcode"] = true;
            BLL.FinanceProp codeNameBll = new BLL.FinanceProp();
            codeNameList = codeNameBll.GetModelList("");
            return View(codeNameList);
        }

        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult FinancePropListPartial()
        {
            BLL.FinanceProp codeNameBll = new BLL.FinanceProp();
            List<Model.FinanceProp> codeNameList = new List<Model.FinanceProp>();
            codeNameList = codeNameBll.GetModelList("");
            ViewData["blcode"] = true;
            return PartialView("FinancePropListPartial", codeNameList);
        }

        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeClsNo">编码类别编码 必须有具体值</param>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult FinancePropEdit(string FFinancePropNo)
        {
            Model.FinanceProp codeNameObj = new Model.FinanceProp();
            ViewData["blcodeNo"] = true;
            if (null != FFinancePropNo)
            {
                FFinancePropNo = Fetch.estimateString(FFinancePropNo, CheckGetEnum.Safety);
                BLL.FinanceProp codeNameBll = new BLL.FinanceProp();
                codeNameObj = codeNameBll.GetModel(FFinancePropNo);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.FinanceProp();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }

            return View(codeNameObj);
        }


        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <param name="codeClsNo">编码类别编码</param>
        /// <param name="isAdd">是否添加true添加，false修改</param>
        /// <returns>是否添加成功</returns>
        public string FinancePropSave(string financePropNo, string financePropName, string fFinancePropShortName, bool isAdd)
        {
            string str = string.Empty;

            financePropNo = Fetch.estimateString(financePropNo, CheckGetEnum.Safety);
            financePropName = Fetch.estimateString(financePropName, CheckGetEnum.Safety);
            fFinancePropShortName = Fetch.estimateString(fFinancePropShortName, CheckGetEnum.Safety);

            BLL.FinanceProp codeNameBll = new BLL.FinanceProp();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(financePropNo))
                {
                    return "exists";
                }
            }

            Model.FinanceProp codeNameObj = new Model.FinanceProp();
            codeNameObj.FFinancePropNo = financePropNo;
            codeNameObj.FFinancePropName = financePropName;
            codeNameObj.FFinancePropShortName = fFinancePropShortName;
            codeNameObj.FYear = decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }


        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeClsNO">编码类别编码</param>
        /// <returns>是否删除成功</returns>
        public string DelFinanceProp(string financePropNo)
        {
            string str = string.Empty;
            if (null != financePropNo)
            {
                financePropNo = Fetch.estimateString(financePropNo, CheckGetEnum.Safety);
                BLL.FinanceProp codeNameBll = new BLL.FinanceProp();
                try
                {
                    codeNameBll.Delete(financePropNo);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
    }
}
